package com.pn.warehouse.service.impl;

import com.pn.warehouse.entity.InStore;
import com.pn.warehouse.entity.Result;
import com.pn.warehouse.mapper.InStoreMapper;
import com.pn.warehouse.mapper.ProductMapper;
import com.pn.warehouse.page.Page;
import com.pn.warehouse.service.InStoreService;
import com.pn.warehouse.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InStoreServiceImpl implements InStoreService{

    @Resource
    private InStoreMapper inStoreMapper;

    @Resource
    private ProductMapper productMapper;

    //分页查询入库单
    @Override
    public Page queryInStorePage(Page page, InStore inStore) {
        //1、查询入库单的行数
        Integer count = inStoreMapper.findInStoreCount(inStore);

        //2、分页查询入库单
        List<InStore> inStoreList = inStoreMapper.findInStorePage(page, inStore);

        //3、封装page信息，并返回
        page.setTotalNum(count);
        page.setResultList(inStoreList);
        return page;
    }

    //确认入库
    @Transactional
    @Override
    public Result inStoreConfirm(InStore inStore) {
        //1、修改商品存库的数量
        int i = productMapper.setInventById(inStore.getProductId(), inStore.getInNum());

        //2、更新入库状态
        int j = inStoreMapper.setIsInById(inStore.getInsId());

        //3、返回响应
        if (i > 0 && j > 0){
            return Result.ok("入库成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "入库失败！");
    }

}
